package com.co.user.service.impl;

import com.co.common.util.IdWorker;
import com.co.user.pojo.History;
import com.co.user.pojo.Later;
import com.co.user.mapper.LaterMapper;
import com.co.user.service.LaterService;
import com.co.video.feign.DetailFeign;
import com.co.video.pojo.Detail;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 * UsLater业务层接口实现类
 */
@Service
public class LaterServiceImpl implements LaterService {

    @Autowired
    private LaterMapper laterMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private DetailFeign detailFeign;


    /**
     * UsLater条件+分页查询
     *
     * @param later 查询条件
     * @param page  页码
     * @param size  页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Later> findPage(Later later, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(later);
        //执行搜索
        return new PageInfo<Later>(laterMapper.selectByExample(example));
    }

    /**
     * UsLater分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Later> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<Later>(laterMapper.selectAll());
    }

    /**
     * UsLater条件查询
     *
     * @param later
     * @return
     */
    @Override
    public List<Later> findList(Later later) {
        //构建查询条件
        Example example = createExample(later);
        //根据构建的条件查询数据
        return laterMapper.selectByExample(example);
    }


    /**
     * UsLater构建查询对象
     *
     * @param later
     * @return
     */
    public Example createExample(Later later) {
        Example example = new Example(Later.class);
        Example.Criteria criteria = example.createCriteria();
        if (later != null) {
            // 主键
            if (!StringUtils.isEmpty(later.getId())) {
                criteria.andEqualTo("id", later.getId());
            }
            // 用户表主键
            if (!StringUtils.isEmpty(later.getUsername())) {
                criteria.andLike("username", "%" + later.getUsername() + "%");
            }
            // vdetail的主键
            if (!StringUtils.isEmpty(later.getAid())) {
                criteria.andEqualTo("aid", later.getAid());
            }
            // 创建时间
            if (!StringUtils.isEmpty(later.getCreateTime())) {
                criteria.andEqualTo("createTime", later.getCreateTime());
            }
            // 修改时间
            if (!StringUtils.isEmpty(later.getUpdateTime())) {
                criteria.andEqualTo("updateTime", later.getUpdateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        laterMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAll() {
        laterMapper.deleteAll();
    }

    /**
     * 修改UsLater
     *
     * @param later
     */
    @Override
    public void update(Later later) {
        laterMapper.updateByPrimaryKey(later);
    }

    /**
     * 增加UsLater
     *
     * @param later
     */
    @Override
    public void add(Later later) {

        //获取用户名
        String username = later.getUsername();
        //获取视频的aid
        String aid = later.getAid();

        Later laterData = laterMapper.selectOne(later);

        if(laterData==null){
            later.setCreateTime(new Date());
            later.setId(String.valueOf(idWorker.nextId()));
            laterMapper.insertSelective(later);
            return;
        }
        if (aid.equals(laterData.getAid())){
            later.setCreateTime(new Date());
            later.setId(laterData.getId());
        }
        laterMapper.updateByPrimaryKey(later);

    }

    /**
     * 根据ID查询UsLater
     *
     * @param id
     * @return
     */
    @Override
    public Later findById(String id) {
        return laterMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询UsLater全部数据
     *
     * @return
     */
    @Override
    public List<Later> findAll() {
        return laterMapper.selectAll();
    }

    /**
     * 通过用户名查询用户稍后观看列表
     * @param username
     * @return
     */
    @Override
    public List<Detail> findLaterByUsername(String username) {
        List<Detail> laterVideoList = new ArrayList<>();

        History history = new History();
        history.setUsername(username);

        //获取later中的aid并以最近优先排序
        List<String> aidList = laterMapper.findLaterByUsername(username);
        for (String aid : aidList) {

            //通过aid查询视频对象
            Detail detail = detailFeign.findById(aid).getData();

            laterVideoList.add(detail);
        }

        return laterVideoList;
    }
}
